package com.polaris.live.popup.party

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.party.ModifyPriceAdapter
import com.polaris.live.adapter.party.ModifyPriceTitleAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.PopupModifyPriceBinding
import com.polaris.live.resp.back_resp.PartyGameResp

/**
 * 游戏价格修改
 * @author Created by 天晴 on 2024/3/19 16:10
 * @since 1.0.0
 **/
class ModifyPricePopup(
    context: Context,
    private val partyGameResp: PartyGameResp,
    private val gameAsset: Int?,
    private val gamePrice: Long?,
    private val onOk: (type: Int, price: Long) -> Unit,
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupModifyPriceBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_modify_price
    }

    private var titlePosition = 0 //头部选中
    private var childPosition = 0 //价格选中

    private val mAdapter by lazy {
        ModifyPriceAdapter()
    }
    private val mTitleAdapter by lazy {
        ModifyPriceTitleAdapter()
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupModifyPriceBinding.bind(popupImplView)

        mAdapter.setOnItemClickListener { _, _, position ->
            childPosition = position
            mAdapter.setClickPosition(childPosition)
        }
        mTitleAdapter.setOnItemClickListener { _, _, position ->
            titlePosition = position
            mTitleAdapter.setClickPosition(titlePosition)

            mAdapter.data.clear()
            childPosition = 0
            partyGameResp.prices[titlePosition].prices?.let {
                mAdapter.addData(it)
                mAdapter.setClickPosition(childPosition)
            }
        }
        mBinding.apply {
            modRecycler.adapter = mAdapter
            modRecycler.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            titleRecycler.adapter = mTitleAdapter
            titleRecycler.layoutManager =
                LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)

            ok.setOnSingleClickListener {
                dismiss()
                onOk.invoke(
                    mTitleAdapter.data[titlePosition].assetType,
                    mAdapter.data.getOrNull(childPosition) ?: 0
                )
            }
            cancel.setOnSingleClickListener {
                dismiss()
            }
        }

        mTitleAdapter.addData(partyGameResp.prices)

        titlePosition = mTitleAdapter.data.indexOfFirst { it.assetType == gameAsset }.let {
            if (it == -1) {
                0
            } else {
                it
            }
        }
        mTitleAdapter.setClickPosition(titlePosition)

        partyGameResp.prices.getOrNull(titlePosition)?.prices?.let { list ->
            mAdapter.addData(list)

            childPosition = mAdapter.data.indexOfFirst { it == gamePrice }.let {
                if (it == -1) {
                    0
                } else {
                    it
                }
            }

            mAdapter.setClickPosition(childPosition)
        }
    }

    companion object {

        fun show(
            context: Context,
            partyGameResp: PartyGameResp,
            gameAsset: Int?,
            gamePrice: Long?,
            onOk: (type: Int, price: Long) -> Unit,
        ) {
            XPopup.Builder(context)
                .isViewMode(true)
                .isDestroyOnDismiss(true)
                .asCustom(ModifyPricePopup(context, partyGameResp, gameAsset, gamePrice, onOk))
                .show()
        }
    }
}